package com.zhuiyun.project.api.appusermessage.service.impl;

import com.zhuiyun.project.api.appusermessage.entity.UserMessage;
import com.zhuiyun.project.api.appusermessage.mapper.UserMessageMapper;
import com.zhuiyun.project.api.appusermessage.model.UserMessageModel;
import com.zhuiyun.project.api.appusermessage.service.UserMessageService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.BeanUtils;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @ClassName UserMessageServiceImpl
 * @Description TODO
 * @Author Root
 * @Date 2023/6/26 11:41
 **/
@Service
public class UserMessageServiceImpl implements UserMessageService {
    @Autowired
    UserMessageMapper userMessageMapper;
    /**
    * @Author Root
    * @Description 用过获取用户信息
    * @Date 2023/6/26 14:38
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param id
    */
    @Override
    public CommonResult getUserById(Integer id) {
        //判断id是否为空
        if(ObjectUtils.isNotEmpty(id)){
            return CommonResult.ok(userMessageMapper.selectByPrimaryKey(id));
        }else{
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
    * @Author Root
    * @Description 用户修改个人信息
    * @Date 2023/6/27 8:35
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param userMessageModel
    */
    @Override
    public CommonResult updateUser(MultipartFile goodsImg, UserMessageModel userMessageModel) {
        //判断图片地址不为空
        if(goodsImg != null){
            String s = UploadFileUtils.uploadAvatar(goodsImg);
            //地址存储为id
            userMessageModel.setImageId(s);
        }
        UserMessage userMessage = new UserMessage();
        // userMessageModel 对象拷贝成 userMessage
        BeanUtils.copy(userMessageModel, userMessage);
        //设置修改时间
        userMessage.setUpdateTime(new Date());
        int i = userMessageMapper.updateByPrimaryKeySelective(userMessage);
        if (i > 0) {
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        } else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
    * @Author Root
    * @Description 修改个人信息
    * @Date 2023/7/24 20:25
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param userMessageModel
    */
    @Override
    public CommonResult updateMessage(  UserMessageModel userMessageModel) {
        UserMessage userMessage = new UserMessage();
        // userMessageModel 对象拷贝成 userMessage
        BeanUtils.copy(userMessageModel, userMessage);
        //设置修改时间
        userMessage.setUpdateTime(new Date());
        int i = userMessageMapper.updateByPrimaryKeySelective(userMessage);
        if (i > 0) {
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        } else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
}
